{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d68fecc6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from glob import glob\n",
    "import os\n",
    "import librosa\n",
    "import soundfile as sf\n",
    "from multiprocess import Pool\n",
    "import itertools\n",
    "\n",
    "def chunks(l, n):\n",
    "    for i in range(0, len(l), n):\n",
    "        yield (l[i: i + n], i // n)\n",
    "\n",
    "def multiprocessing(strings, function, cores=6, returned=True):\n",
    "    df_split = chunks(strings, len(strings) // cores)\n",
    "    pool = Pool(cores)\n",
    "    pooled = pool.map(function, df_split)\n",
    "    pool.close()\n",
    "    pool.join()\n",
    "\n",
    "    if returned:\n",
    "        return list(itertools.chain(*pooled))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ecbc5224",
   "metadata": {},
   "outputs": [],
   "source": [
    "!mkdir CochlScene-audio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a53b7e08",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[0m\u001b[01;34mTest\u001b[0m/  \u001b[01;34mTrain\u001b[0m/  \u001b[01;34mVal\u001b[0m/\r\n"
     ]
    }
   ],
   "source": [
    "ls CochlScene"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3fde473f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "def loop(files):\n",
    "    files, _ = files\n",
    "    data = []\n",
    "    for f in tqdm(files):\n",
    "        audio_filename = os.path.join('CochlScene-audio', f.replace('/', '_')).replace('.wav', '.mp3')\n",
    "        if not os.path.exists(audio_filename):\n",
    "            y, sr = librosa.load(f, sr = 16000)\n",
    "            sf.write(audio_filename, y, sr)\n",
    "        \n",
    "        data.append({\n",
    "            'audio_filename': audio_filename,\n",
    "            'answer': os.path.split(f)[1].split('_')[0],\n",
    "            'metadata': ''\n",
    "        })\n",
    "    return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0686aa33",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|                                                                                              | 0/3042 [00:00<?, ?it/s]/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "  0%|                                                                                              | 0/3042 [00:00<?, ?it/s]/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "  0%|                                                                                              | 0/3042 [00:00<?, ?it/s]/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "  0%|                                                                                              | 0/3042 [00:00<?, ?it/s]/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "  0%|                                                                                              | 0/3042 [00:00<?, ?it/s]/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "  0%|                                                                                              | 0/3042 [00:00<?, ?it/s]/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:09<00:00, 16.07it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:09<00:00, 16.02it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████████| 15/15 [00:00<00:00, 16.08it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:10<00:00, 15.99it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:10<00:00, 15.98it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:11<00:00, 15.88it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:11<00:00, 15.88it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:11<00:00, 15.88it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:12<00:00, 15.82it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:12<00:00, 15.84it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:12<00:00, 15.84it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:12<00:00, 15.81it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:12<00:00, 15.77it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:12<00:00, 15.77it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:13<00:00, 15.75it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:13<00:00, 15.72it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:13<00:00, 15.69it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:14<00:00, 15.65it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:14<00:00, 15.65it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:15<00:00, 15.60it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████| 3042/3042 [03:15<00:00, 15.53it/s]\n"
     ]
    }
   ],
   "source": [
    "train_files = glob('CochlScene/Train/*/*.wav')\n",
    "train = multiprocessing(train_files, loop, cores = 20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "58e4446e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "60855"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "9ceeae3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'audio_filename': 'CochlScene-audio/CochlScene_Train_Street_Street_user0765_14844491_001.mp3',\n",
       " 'answer': 'Street',\n",
       " 'metadata': ''}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "9fb202b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "labels = str(list(set([r['answer'] for r in train])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "3f3b6196",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "questions = [\n",
    "    'given the labels\\n{labels}\\n\\nclassify the audio',\n",
    "    'what is the label for audio\\n\\nthe labels: {labels}'\n",
    "]\n",
    "\n",
    "for i in range(len(train)):\n",
    "    train[i]['question'] = random.choice(questions).format(labels = labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "6a080a6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'audio_filename': 'CochlScene-audio/CochlScene_Train_Street_Street_user0765_14844491_001.mp3',\n",
       " 'answer': 'Street',\n",
       " 'metadata': '',\n",
       " 'question': \"given the labels\\n['Park', 'Car', 'Kitchen', 'Restaurant', 'Subway', 'Cafe', 'Elevator', 'Street', 'SubwayStation', 'Bus', 'ResidentialArea', 'CrowdedIndoor', 'Restroom']\\n\\nclassify the audio\"}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "1da083f0",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|                                                                                               | 0/384 [00:00<?, ?it/s]/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:25<00:00, 15.04it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████████| 7/7 [00:00<00:00, 16.36it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:26<00:00, 14.67it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:26<00:00, 14.40it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:26<00:00, 14.39it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:26<00:00, 14.31it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:26<00:00, 14.24it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:26<00:00, 14.23it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:27<00:00, 14.19it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:27<00:00, 14.17it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:27<00:00, 14.15it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:27<00:00, 14.15it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:27<00:00, 14.10it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:27<00:00, 14.05it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:27<00:00, 14.01it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:27<00:00, 14.01it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:27<00:00, 14.02it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:27<00:00, 13.99it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:27<00:00, 13.98it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:27<00:00, 13.98it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 384/384 [00:27<00:00, 13.98it/s]\n"
     ]
    }
   ],
   "source": [
    "test_files = glob('CochlScene/Test/*/*.wav')\n",
    "test = multiprocessing(test_files, loop, cores = 20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "91257daf",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(len(test)):\n",
    "    test[i]['question'] = random.choice(questions).format(labels = labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "0543404b",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|                                                                                               | 0/378 [00:00<?, ?it/s]/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "/usr/lib/python3/dist-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.17.3 and <1.25.0 is required for this version of SciPy (detected version 1.26.4\n",
      "  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 378/378 [00:25<00:00, 14.64it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 378/378 [00:26<00:00, 14.35it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 378/378 [00:26<00:00, 14.23it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 378/378 [00:26<00:00, 14.23it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 378/378 [00:26<00:00, 14.22it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 378/378 [00:26<00:00, 14.22it/s]\n",
      "100%|████████████████████████████████████████████████████████████████████████████████████▊| 377/378 [00:26<00:00, 16.00it/s]\n",
      "100%|███████████████████████████████████████████████████████████████████████████████████████| 13/13 [00:00<00:00, 15.84it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 378/378 [00:26<00:00, 14.18it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 378/378 [00:26<00:00, 14.16it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 378/378 [00:26<00:00, 14.13it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 378/378 [00:26<00:00, 14.11it/s]\n",
      "100%|████████████████████████████████████████████████████████████████████████████████████▊| 377/378 [00:26<00:00, 15.87it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 378/378 [00:26<00:00, 14.12it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 378/378 [00:26<00:00, 14.09it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 378/378 [00:26<00:00, 14.06it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 378/378 [00:26<00:00, 14.06it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 378/378 [00:26<00:00, 14.02it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 378/378 [00:26<00:00, 14.00it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 378/378 [00:27<00:00, 13.97it/s]\n",
      "100%|█████████████████████████████████████████████████████████████████████████████████████| 378/378 [00:27<00:00, 13.95it/s]\n"
     ]
    }
   ],
   "source": [
    "val_files = glob('CochlScene/Val/*/*.wav')\n",
    "val = multiprocessing(val_files, loop, cores = 20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "1cd26848",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(len(val)):\n",
    "    val[i]['question'] = random.choice(questions).format(labels = labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "731e5704",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(60855, 7687, 7573)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(train), len(test), len(val)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "3c1fb029",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7b6dbb8935f8494cac10c0b14856f45d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading the dataset shards:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "63741f2669684e50aa225e2539ed11da",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating parquet from Arrow format:   0%|          | 0/61 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "059ef1d151ff483d9729fbf3b52d41d2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading...:   0%|          | 0.00/936k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d8cc693ed21d449eaa77f5686f598677",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "README.md:   0%|          | 0.00/2.97k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "CommitInfo(commit_url='https://huggingface.co/datasets/mesolitica/Zeroshot-Audio-Classification-Instructions/commit/2483209148bcf8132d2859123332cbc234edc1e9', commit_message='Upload dataset', commit_description='', oid='2483209148bcf8132d2859123332cbc234edc1e9', pr_url=None, repo_url=RepoUrl('https://huggingface.co/datasets/mesolitica/Zeroshot-Audio-Classification-Instructions', endpoint='https://huggingface.co', repo_type='dataset', repo_id='mesolitica/Zeroshot-Audio-Classification-Instructions'), pr_revision=None, pr_num=None)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datasets import Dataset\n",
    "\n",
    "dataset = Dataset.from_list(train)\n",
    "dataset.push_to_hub('mesolitica/Zeroshot-Audio-Classification-Instructions', split = 'CochlScene_train')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "8c593a72",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "03012dd9f29744eb99f9ef3d92eb3628",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading the dataset shards:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a3ea3e0ece2b4ecf8767719d17be7299",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating parquet from Arrow format:   0%|          | 0/8 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "539a218745d24b3483ff6e5eb74b3eca",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading...:   0%|          | 0.00/104k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f0c0b974499147a1b05f32227a926a99",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "README.md:   0%|          | 0.00/3.11k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "CommitInfo(commit_url='https://huggingface.co/datasets/mesolitica/Zeroshot-Audio-Classification-Instructions/commit/d17921028984254584ef312355b49163079422ed', commit_message='Upload dataset', commit_description='', oid='d17921028984254584ef312355b49163079422ed', pr_url=None, repo_url=RepoUrl('https://huggingface.co/datasets/mesolitica/Zeroshot-Audio-Classification-Instructions', endpoint='https://huggingface.co', repo_type='dataset', repo_id='mesolitica/Zeroshot-Audio-Classification-Instructions'), pr_revision=None, pr_num=None)"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset = Dataset.from_list(test)\n",
    "dataset.push_to_hub('mesolitica/Zeroshot-Audio-Classification-Instructions', split = 'CochlScene_test')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "c0959473",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "64980ef754d84c9890b59eeb0e15346b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading the dataset shards:   0%|          | 0/1 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d55a571a1fc64998982182cbe950fb3f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Creating parquet from Arrow format:   0%|          | 0/8 [00:00<?, ?ba/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b4e7d979748d43da8fc578cf6e0a4dd9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Uploading...:   0%|          | 0.00/99.9k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "528d46a79450458097d3ed7591eae6fb",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "README.md:   0%|          | 0.00/3.24k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "CommitInfo(commit_url='https://huggingface.co/datasets/mesolitica/Zeroshot-Audio-Classification-Instructions/commit/9d8fb45810e2c7e2980a2e526b29832855eca3c1', commit_message='Upload dataset', commit_description='', oid='9d8fb45810e2c7e2980a2e526b29832855eca3c1', pr_url=None, repo_url=RepoUrl('https://huggingface.co/datasets/mesolitica/Zeroshot-Audio-Classification-Instructions', endpoint='https://huggingface.co', repo_type='dataset', repo_id='mesolitica/Zeroshot-Audio-Classification-Instructions'), pr_revision=None, pr_num=None)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset = Dataset.from_list(val)\n",
    "dataset.push_to_hub('mesolitica/Zeroshot-Audio-Classification-Instructions', split = 'CochlScene_val')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "a0c6f53c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "76115"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from glob import glob\n",
    "\n",
    "audio_files = glob('CochlScene-audio/*.mp3')\n",
    "len(audio_files)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "c9566840",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.3G\tCochlScene-audio\r\n"
     ]
    }
   ],
   "source": [
    "!du -hs CochlScene-audio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "ea2c8b01",
   "metadata": {},
   "outputs": [],
   "source": [
    "import zipfile\n",
    "\n",
    "with zipfile.ZipFile('CochlScene-audio.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:\n",
    "    for f in audio_files:\n",
    "        zipf.write(f, arcname=f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "3fa2bed1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Uploading files using Xet Storage..\n",
      "Uploading...: 100%|████████████████████████| 3.32G/3.32G [00:54<00:00, 61.2MB/s]\n",
      "https://huggingface.co/datasets/mesolitica/Zeroshot-Audio-Classification-Instructions/blob/main/CochlScene-audio.zip\n"
     ]
    }
   ],
   "source": [
    "!huggingface-cli upload mesolitica/Zeroshot-Audio-Classification-Instructions CochlScene-audio.zip \\\n",
    "--repo-type=dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "037cc844",
   "metadata": {},
   "outputs": [],
   "source": [
    "!rm CochlScene-audio.zip"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
